/**
 * Created by jack on 2017/12/14.
 */
'use strict';

goog.provide('Blockly.JavaScript.logic');

goog.require('Blockly.JavaScript');


Blockly.JavaScript.control_repeat = function() {
  var value_name = Blockly.JavaScript.valueToCode(this, 'TIMES', Blockly.JavaScript.ORDER_ATOMIC);
  var statements_name = Blockly.JavaScript.statementToCode(this, 'SUBSTACK');
  // TODO: Assemble JavaScript into code variable.
  var code = 'for (var i=0;i<' + value_name + ';i++) {\n' + statements_name + '}\n';
  return code;
};

Blockly.JavaScript.control_if = function() {
  // If condition.
  var value_name = Blockly.JavaScript.valueToCode(this, 'CONDITION', Blockly.JavaScript.ORDER_ATOMIC);
  var statements_ccc = Blockly.JavaScript.statementToCode(this, 'SUBSTACK');
  if(value_name==null || value_name==""){
    value_name=false;
  }
  // TODO: Assemble JavaScript into code variable.
  var code = 'if ('+value_name+') {\n'+statements_ccc+'}\n';
  return code;
};


Blockly.JavaScript.control_if_else = function() {
  // If condition.
  var value_name = Blockly.JavaScript.valueToCode(this, 'CONDITION', Blockly.JavaScript.ORDER_ATOMIC);
  var statements_if = Blockly.JavaScript.statementToCode(this, 'SUBSTACK');
  var statements_else = Blockly.JavaScript.statementToCode(this, 'SUBSTACK2');
  if(value_name==null || value_name==""){
    value_name=false;
  }
  // TODO: Assemble JavaScript into code variable.
  var code = 'if ('+value_name+') {\n'+statements_if+'}\nelse {\n'+statements_else+'}\n';
  return code;
};

Blockly.JavaScript.control_repeat_until = function() {
  var value_name = Blockly.JavaScript.valueToCode(this, 'CONDITION', Blockly.JavaScript.ORDER_ATOMIC);
  var statements_name = Blockly.JavaScript.statementToCode(this, 'SUBSTACK');
  if(value_name==null || value_name==""){
    value_name=false;
  }
  // TODO: Assemble JavaScript into code variable.
  var code = 'while (true) {\n' + statements_name + '\nif('+value_name+') {\n  break;\n  }\n}\n';
  return code;
};

Blockly.JavaScript.control_jump_out_loop = function() {
    //跳出循环
  var code = 'break;\n';
  return code;
};
